package com.iot.inter.controller.newinterface.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.DeviceRealTimeInformation;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.device.singledevicerealtime.request.RequestSingleDeviceRealTime;
import com.iot.inter.entity.newrequest.device.singledevicerealtime.response.ResponseSingleDeviceRealTime;
import com.iot.inter.entity.newrequest.device.singledevicerealtime.response.ResponseSingleDeviceRealTimeTradeData;
import com.iot.inter.entity.newrequest.device.singledevicerealtime.response.ResponseSingleDeviceRealTimeTradeDataPackageOrder;
import com.iot.inter.entity.newrequest.device.singledevicerealtime.response.ResponseSingleDeviceRealTimeTradeDataPackageOrderRefuelingPackage;
import com.iot.inter.interfaceprocessing.newinterface.TurnToDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.3查询单一设备实时信息：2.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.3查询单一设备实时信息")
public class SingleDeviceQueryRealTimeController {
    private static Logger logger = Logger.getLogger(SingleDeviceQueryRealTimeController.class);

    @CrossOrigin
    @RequestMapping(value = "singleDeviceRealTime", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.3查询单一设备实时信息", response = ResponseSingleDeviceRealTime.class)
    public String singleDeviceRealTime(@ApiParam @RequestBody RequestSingleDeviceRealTime request) {
        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {
            logger.info("【查询单一设备实时信息】请求" + requestStr);

            RequestSingleDeviceRealTime requestPackage = JSONArray.parseObject(requestStr, RequestSingleDeviceRealTime.class);
            String deviceId = requestPackage.getDeviceId();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            //现在去北京查询单一设备实时信息
            TurnToDeviceService realTimeService = new TurnToDeviceService();
            DeviceRealTimeInformation manager = realTimeService.singleDeviceRealTime(deviceId, secret, partnerCode);

            if (null != manager) {
                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(manager.getRetCode())) {
                    if (null != manager.getRefPackageName()) {
                        //这边说明拿到了成功的0000返回，是成功的
                        List<ResponseSingleDeviceRealTimeTradeDataPackageOrderRefuelingPackage> refuelingPackage = new ArrayList<ResponseSingleDeviceRealTimeTradeDataPackageOrderRefuelingPackage>();
                        ResponseSingleDeviceRealTimeTradeDataPackageOrderRefuelingPackage refueling = new ResponseSingleDeviceRealTimeTradeDataPackageOrderRefuelingPackage();
                        refueling.setRefOrderId(manager.getRefOrderId());
                        refueling.setRefpackageStatus(manager.getRefpackageStatus());
                        refueling.setRefPackageFlow(manager.getRefPackageFlow());
                        refueling.setRefpackageOrderDate(manager.getRefpackageOrderDate());
                        refuelingPackage.add(refueling);

                        List<ResponseSingleDeviceRealTimeTradeDataPackageOrder> packageOrder = new ArrayList<ResponseSingleDeviceRealTimeTradeDataPackageOrder>();
                        ResponseSingleDeviceRealTimeTradeDataPackageOrder order = new ResponseSingleDeviceRealTimeTradeDataPackageOrder();
                        order.setOrderCode(manager.getOrderCode());
                        order.setPackageName(manager.getPackageName());
                        order.setPackageCode(manager.getPackageCode());
                        order.setPackageType(manager.getPackageType());
                        order.setBuyType(manager.getBuyType());
                        order.setOrderPeriod(manager.getOrderPeriod());
                        order.setFlow(manager.getFlow());
                        order.setUsedFlow(manager.getUsedFlow());
                        order.setFlowUseRate(manager.getFlowUseRate());
                        order.setOrderUsedFlow(manager.getOrderUsedFlow());
                        order.setOrderDate(manager.getOrderDate());
                        order.setActiveDate(manager.getActiveDate());
                        order.setExpireDate(manager.getExpireDate());
                        order.setIsOrderRefuelingPackage(manager.getIsOrderRefuelingPackage());
                        order.setRefuelingPackage(refuelingPackage);
                        packageOrder.add(order);

                        List<ResponseSingleDeviceRealTimeTradeData> timeTradeData = new ArrayList<ResponseSingleDeviceRealTimeTradeData>();
                        ResponseSingleDeviceRealTimeTradeData tra = new ResponseSingleDeviceRealTimeTradeData();
                        tra.setDeviceId(manager.getDeviceId());
                        tra.setOtaFlag(manager.getOtaFlag());
                        tra.setBipFlag(manager.getBipFlag());
                        tra.setMultiImsiFlag(manager.getMultiImsiFlag());
                        tra.setLifecycle(manager.getLifecycle());
                        tra.setLifecycleStartTime(manager.getLifecycleStartTime());
                        tra.setLifecycleEndTime(manager.getLifecycleEndTime());
                        tra.setLifecycleSlientPeriod(manager.getLifecycleSlientPeriod());
                        tra.setLifecycleShutdownPeriod(manager.getLifecycleShutdownPeriod());
                        tra.setPackageOrder(packageOrder);
                        timeTradeData.add(tra);

                        ResponseSingleDeviceRealTime deviceRealTime = new ResponseSingleDeviceRealTime();
                        deviceRealTime.setTradeData(timeTradeData);
                        deviceRealTime.setRetCode(manager.getRetCode());
                        deviceRealTime.setResultInfo(manager.getResultInfo());
                        // 返回给调用者的报文
                        jsonString = JSON.toJSONString(deviceRealTime);
                        logger.info("【查询单一设备实时信息】成功响应：" + jsonString);
                        return jsonString;
                    } else {
                        List<ResponseSingleDeviceRealTimeTradeDataPackageOrder> packageOrder = new ArrayList<ResponseSingleDeviceRealTimeTradeDataPackageOrder>();
                        ResponseSingleDeviceRealTimeTradeDataPackageOrder order = new ResponseSingleDeviceRealTimeTradeDataPackageOrder();
                        order.setOrderCode(manager.getOrderCode());
                        order.setPackageName(manager.getPackageName());
                        order.setPackageCode(manager.getPackageCode());
                        order.setPackageType(manager.getPackageType());
                        order.setBuyType(manager.getBuyType());
                        order.setOrderPeriod(manager.getOrderPeriod());
                        order.setFlow(manager.getFlow());
                        order.setUsedFlow(manager.getUsedFlow());
                        order.setFlowUseRate(manager.getFlowUseRate());
                        order.setOrderUsedFlow(manager.getOrderUsedFlow());
                        order.setOrderDate(manager.getOrderDate());
                        order.setActiveDate(manager.getActiveDate());
                        order.setExpireDate(manager.getExpireDate());
                        order.setIsOrderRefuelingPackage(manager.getIsOrderRefuelingPackage());
                        packageOrder.add(order);

                        List<ResponseSingleDeviceRealTimeTradeData> timeTradeData = new ArrayList<ResponseSingleDeviceRealTimeTradeData>();
                        ResponseSingleDeviceRealTimeTradeData tra = new ResponseSingleDeviceRealTimeTradeData();
                        tra.setDeviceId(manager.getDeviceId());
                        tra.setOtaFlag(manager.getOtaFlag());
                        tra.setBipFlag(manager.getBipFlag());
                        tra.setMultiImsiFlag(manager.getMultiImsiFlag());
                        tra.setLifecycle(manager.getLifecycle());
                        tra.setLifecycleStartTime(manager.getLifecycleStartTime());
                        tra.setLifecycleEndTime(manager.getLifecycleEndTime());
                        tra.setLifecycleSlientPeriod(manager.getLifecycleSlientPeriod());
                        tra.setLifecycleShutdownPeriod(manager.getLifecycleShutdownPeriod());
                        tra.setPackageOrder(packageOrder);
                        timeTradeData.add(tra);

                        ResponseSingleDeviceRealTime deviceRealTime = new ResponseSingleDeviceRealTime();
                        deviceRealTime.setTradeData(timeTradeData);
                        deviceRealTime.setRetCode(manager.getRetCode());
                        deviceRealTime.setResultInfo(manager.getResultInfo());
                        // 返回给调用者的报文
                        jsonString = JSON.toJSONString(deviceRealTime);
                        logger.info("【查询单一设备实时信息】成功响应：" + jsonString);
                        return jsonString;
                    }

                } else {

                    ResponseSingleDeviceRealTime responseOrder = new ResponseSingleDeviceRealTime();
                    responseOrder.setRetCode(manager.getRetCode());
                    responseOrder.setResultInfo(manager.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【查询单一设备实时信息】响应：" + jsonString);
                    return jsonString;
                }

            } else {
                ResponseSingleDeviceRealTime responseOrder = new ResponseSingleDeviceRealTime();
                responseOrder.setRetCode("9998");
                responseOrder.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【查询单一设备实时信息】响应：" + jsonString);
                return jsonString;
            }
        } else {
            ResponseSingleDeviceRealTime responseOrder = new ResponseSingleDeviceRealTime();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【查询单一设备实时信息】响应：" + jsonString);
            return jsonString;
        }
    }


}
